<template>
  <div class="home">
    <el-main id="content">
      <el-form label-position="right" label-width="80px" :model="formdata" ref="form" :rules="rules"  status-icon>
        <el-form-item label="账号" prop="user">
          <el-input v-model="formdata.user" type="text"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="formdata.password" type="password"></el-input>
        </el-form-item>
      </el-form>
      <el-row >
        <el-col :span="12"><el-button type="primary" @click="validates()">登录</el-button></el-col>
        <el-col :span="12"><el-button type="warning">注册</el-button></el-col>
      </el-row>
      <el-row>
        <el-button type="primary" @click="visitor_login()">游客体验</el-button>
      </el-row>
    </el-main>
    <el-footer>
      <a class="icp" href="http://www.miitbeian.gov.cn/" rel="external nofollow" target="_blank">豫ICP备2021001555号-1</a>
    </el-footer>
  </div>
</template>

<script>
// @ is an alias to /src

export default {
  name: 'Home',
  data() {
     let user_rules = (rule, value, callback) => {
        let rex=/(\W)|([\_])$/
        if (value === '') {
          callback(new Error('请输入账号'));
        } else if (value.length < 2 ) {
          callback(new Error('账号长度不对!'));
        } else if (rex.test(value)) {
          callback(new Error("账号不能包含特殊字符"));
        } else{
          callback()
        }
        }
    return {
      formdata:{
        user:"",
        password:""
      },
      rules:{
        user:[
          {validator:user_rules,trigger:'blur'}
        ],
        password:[
          { required: true, message: '请输入密码', trigger: 'blur' },
        ]
      }
    }
  },
  methods:{
    validates() {
      this.$refs["form"].validate((valid)=>{
        if(!valid){
          
          return
        }
      })
      this.$axios
        .post(
          this.$api+"login",
          this.$qs.stringify(this.formdata)
        )
        .then((res) => {
          console.log(res);
          let data=res.data.data
          if (res.data.status == 200) {
            sessionStorage.setItem("user_id",data.user_id)
            sessionStorage.setItem("theme",data.theme)
            sessionStorage.setItem("head_icon",data.head_icon)
            sessionStorage.setItem('form',JSON.stringify(data))
            this.$router.push("/About");
            console.log("haha");
          } else {
            alert("账号或者密码错误");
          }
        })
        .catch((err) => null);
    },
    visitor_login(){
      this.formData={
        user: "159",
        password: "159",
      }
      this.validates()
    },
    register(){
      this.$router.push('regiser')
      console.log("jump");
    }
  
  }
}
</script>
<style>
#content{
      width: 45%;
    margin: 0 auto;
    margin-top: 15%;
    background-color: cornflowerblue;
    border-radius: 30px;
    max-width: 600px;
}
.el-footer{
    position: absolute;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
}
.el-button{
      width: 60%;
}
.el-row{
  padding: 10px;
}
.el-form{
  padding-right: 10%;
}
</style>